# -*- coding: utf-8 -*-
'''
@author: Donny
@Created on: 2011-8-19
@version: 2011.8.19
@summary: Definition of report class
'''
from util.util import Util
from util.xmlparse import XmlParser
from util.databasehandler import DatabaseHandler
root = Util.get_root_path()

class Reporter(object):
    '''
    Report result to database and send summary email.
    '''
    def __init__(self, configuer):
        '''
        Constructor.
        '''
        print 'reporter -- init '
        self.configuer = configuer
    
    def report_database(self, case_list):
        '''
        Repport results to database.
        @params:
        case_list -- case information list
        '''
        print 'reporter -- report_database '
        for case in case_list:
            case_name = case[0]
            xml_name = root + "/Results/" + case_name + "_results.xml"
            result_list = self.get_results(xml_name)
            print 'reporter -- report_database - result_list: ' \
            + str(result_list)
            self.upload_results(result_list)
        
    def get_results(self, file_name):
        '''
        Get one case results from xml result file.
        ''' 
        print 'reporter -- get_results '
        xml_parser = XmlParser(file_name)
        result_list = xml_parser.read_xml_report()
        return result_list
        
    def upload_results(self, result_list):
        '''
        Upload one case results to database.
        ''' 
        print 'reporter -- upload_results: result_list = ' + str(result_list)
        host = self.configuer.db_host
        database = self.configuer.database
        user = self.configuer.db_user
        password = self.configuer.db_password
        table = self.configuer.table
        db_handler = DatabaseHandler(host, user, password, database)
        
        data_dict = {}
        data_dict["CaseId"] = result_list.pop(0)
        data_dict["TimeStamp"] = result_list.pop(0)
        for step in result_list:
            data_dict["StepName"] = step.name
            data_dict["StepDescription"] = step.description
            data_dict["ExpectedResult"] = step.expected_result
            data_dict["StepResult"] = step.result
            data_dict["TestLog"] = 'test log information'#step.test_log
            data_dict["Screenshot"] = step.step_pic
            db_handler.insert(table, data_dict)
        print 'reporter -- upload_results: report done! '
        